package com.xd.server.apply.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.common.page.Pagination;
import com.xd.common.utils.OrikaUtils;
import com.xd.server.apply.dto.ApplyDTO;
import com.xd.server.apply.mapper.IApplyMapper;
import com.xd.server.apply.po.ApplyPO;
import com.xd.server.apply.service.ApplyService;
import com.xd.server.apply.vo.ApplyVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 申请表(Apply)表服务实现类
 *
 * @author XD
 * @since 2021-07-10 13:38:09
 */
@Service("applyService")
public class ApplyServiceImpl extends ServiceImpl<IApplyMapper, ApplyPO> implements ApplyService {
    @Resource
    private IApplyMapper applyMapper;

    /**
     * 增加
     *
     * @param applyDTO DTO传输类
     */
    @Override
    public int saveOrUpdateApply(ApplyDTO applyDTO) {
        // 根据类型特殊处理
        Long id = applyDTO.getId();
        final ApplyPO applyPO = OrikaUtils.map(applyDTO, ApplyPO.class);
        if (StringUtils.isEmpty(id)) {
            // 新增
            return applyMapper.insert(applyPO);
        } else {
            // 修改
            return applyMapper.updateById(applyPO);
        }


    }

    /**
     * 删除
     *
     * @param id 主键
     */
    @Override
    public int deleteApply(String id) {
        return applyMapper.deleteById(id);

    }


    /**
     * 查询一个
     *
     * @param id 主键
     * @return com.xd.vo.ApplyVO
     */
    @Override
    public ApplyVO findApply(String id) {
        final ApplyPO applyPO = applyMapper.selectById(id);
        return OrikaUtils.map(applyPO, ApplyVO.class);
    }

    /**
     * 查询所有
     *
     * @return java.util.List
     */
    @Override
    public List<ApplyVO> findAllApply() {
        final List<ApplyPO> applyPOS = applyMapper.selectList(null);
        return OrikaUtils.mapAsList(applyPOS, ApplyVO.class);
    }

    /**
     * 查询所有-分页查询
     *
     * @return java.util.List
     */
    @Override
    public Page<ApplyVO> findPageApply(ApplyDTO applyDTO) {
        Page<ApplyVO> page = Pagination.page(applyDTO);
        List<ApplyVO> ApplyVOS = applyMapper.findAll(page, applyDTO);
        return page.setRecords(ApplyVOS);
    }

}
